package com.paobuji.aidebao.myapplication.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.MainActivity;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.bean.BluResult;
import com.paobuji.aidebao.myapplication.ble.utils.BluetoothController;
import com.paobuji.aidebao.myapplication.ble.utils.ConstantUtils;
import com.paobuji.aidebao.myapplication.ble.utils.Util;
import com.paobuji.aidebao.myapplication.tool.ACache;
import com.paobuji.aidebao.myapplication.tool.BluDataUtil;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;
import com.shawnlin.numberpicker.NumberPicker;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MySportModelActivity extends BaseActivity {


    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;
    @BindView(R.id.daoTimeLayout)
    RelativeLayout daoTimeLayout;
    @BindView(R.id.daoCorLayout)
    RelativeLayout daoCorLayout;
    @BindView(R.id.daoDisLayout)
    RelativeLayout daoDisLayout;
    @BindView(R.id.pitchImg)
    ImageView pitchImg;
    @BindView(R.id.horizontal_number_picker)
    NumberPicker horizontalNumberPicker;
    @BindView(R.id.startBut)
    Button startBut;
    @BindView(R.id.pitchText)
    TextView pitchText;

    String model = "time";
    int num = 0;
    Util u = new Util();
    BluResult bluResult;
    ACache aCache;

    @BindView(R.id.daoTimeImg)
    FloatingActionButton daoTimeImg;
    @BindView(R.id.daoCorImg)
    FloatingActionButton daoCorImg;
    @BindView(R.id.daoTimeText)
    TextView daoTimeText;
    @BindView(R.id.daoCorText)
    TextView daoCorText;
    @BindView(R.id.daoDisText)
    TextView daoDisText;
    @BindView(R.id.daoDisImg)
    FloatingActionButton daoDisImg;
    BluetoothController controller;
    BluDataUtil bluDataUtil = new BluDataUtil();

    boolean isRun = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sport_model);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);

        aCache = ACache.get(this);
        controller = BluetoothController.getInstance();
        horizontalNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                num = newVal;
            }
        });

        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

        //进入模式设置界面，默认倒计时模式设置
        timeSet();

        inidBle();

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.daoTimeLayout, R.id.daoCorLayout, R.id.daoDisLayout, R.id.startBut, R.id.daoTimeImg, R.id.daoCorImg, R.id.daoDisImg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.daoTimeLayout:
                model = "time";
                timeSet();
                break;
            case R.id.daoCorLayout:
                model = "cor";
                corSet();
                break;
            case R.id.daoDisLayout:
                model = "dis";
                disSet();
                break;
            case R.id.startBut:
                aCache.put("model", model);
                aCache.put("daonum", num+"");
                aCache.put("modelboolean","true");
                goSport();
                break;
            case R.id.daoTimeImg:
                timeSet();
                break;
            case R.id.daoCorImg:
                corSet();
                break;
            case R.id.daoDisImg:
                disSet();
                break;
        }
    }

    public void goSport() {
        if(!isRun){
            controller.write(bluDataUtil.startStop("1"));
            Intent intent = new Intent(MySportModelActivity.this, VedioActivity.class);
            startActivity(intent);
            finish();
        }else {
            Toast.makeText(MySportModelActivity.this,R.string.stop_running,Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 倒计时模式界面设置
     */
    public void timeSet(){
        horizontalNumberPicker.setMaxValue(99);
        horizontalNumberPicker.setMinValue(10);
        horizontalNumberPicker.setValue(30);

        daoTimeImg.setImageResource(R.mipmap.timeb);
        daoCorImg.setImageResource(R.mipmap.cord);
        daoDisImg.setImageResource(R.mipmap.disd);

        daoTimeText.setTextColor(getResources().getColor(R.color.timeColor));
        daoCorText.setTextColor(getResources().getColor(R.color.textColor));
        daoDisText.setTextColor(getResources().getColor(R.color.textColor));

        pitchText.setTextColor(getResources().getColor(R.color.timeColor));
        pitchText.setText(getResources().getString(R.string.time) + "/min");
        Glide.with(this)
                // .load(mResultsPath.get(0))
                .load(R.mipmap.timeb)
                .asBitmap()
                .skipMemoryCache(true) //跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                .placeholder(R.mipmap.timeb)    //占位图
                .error(R.mipmap.timeb)
                .into(pitchImg);

        model = "time";
    }

    /**
     * 倒计卡路里模式界面设置
     */
    public void corSet(){
        horizontalNumberPicker.setMaxValue(550);
        horizontalNumberPicker.setMinValue(20);
        horizontalNumberPicker.setValue(100);

        daoTimeImg.setImageResource(R.mipmap.timed);
        daoCorImg.setImageResource(R.mipmap.corb);
        daoDisImg.setImageResource(R.mipmap.disd);

        daoTimeText.setTextColor(getResources().getColor(R.color.textColor));
        daoCorText.setTextColor(getResources().getColor(R.color.corColor));
        daoDisText.setTextColor(getResources().getColor(R.color.textColor));

        pitchText.setTextColor(getResources().getColor(R.color.corColor));
        pitchText.setText(getResources().getString(R.string.calorie) + "/Kal");
        Glide.with(this)
                // .load(mResultsPath.get(0))
                .load(R.mipmap.corb)
                .asBitmap()
                .skipMemoryCache(true) //跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                .placeholder(R.mipmap.corb)    //占位图
                .error(R.mipmap.corb)
                .into(pitchImg);
        model = "cor";
    }


    /**
     * 倒计距离模式界面设置
     */
    public void disSet(){
        horizontalNumberPicker.setMaxValue(99);
        horizontalNumberPicker.setMinValue(1);
        horizontalNumberPicker.setValue(5);

        daoTimeImg.setImageResource(R.mipmap.timed);
        daoCorImg.setImageResource(R.mipmap.cord);
        daoDisImg.setImageResource(R.mipmap.disb);

        daoTimeText.setTextColor(getResources().getColor(R.color.textColor));
        daoCorText.setTextColor(getResources().getColor(R.color.textColor));
        daoDisText.setTextColor(getResources().getColor(R.color.disColor));

        pitchText.setTextColor(getResources().getColor(R.color.disColor));
        pitchText.setText(getResources().getString(R.string.dis) + "/Km");
        Glide.with(this)
                // .load(mResultsPath.get(0))
                .load(R.mipmap.disb)
                .asBitmap()
                .skipMemoryCache(true) //跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                .placeholder(R.mipmap.disb)    //占位图
                .error(R.mipmap.disb)
                .into(pitchImg);

        model = "dis";
    }
MsgReceiver receiver;
    //判断蓝牙是否已经连接
    //在跳转前开始跑步，在跳转
    public void inidBle(){
        //注册广播
        receiver = new MsgReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConstantUtils.ACTION_RECEIVE_MESSAGE_FROM_DEVICE);
        intentFilter.addAction(ConstantUtils.ACTION_STOP_CONNECT);
        registerReceiver(receiver, intentFilter);
    }
    /**
     * 接收广播，接收蓝牙模块发过来的值
     */
    public class MsgReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equalsIgnoreCase(ConstantUtils.ACTION_STOP_CONNECT)) {
                Log.i("蓝牙", "连接已断开");
            } else if (intent.getAction().equalsIgnoreCase(ConstantUtils.ACTION_RECEIVE_MESSAGE_FROM_DEVICE)) {
                try{
                    bluResult = bluDataUtil.SplitStr(u.toStringHex(intent.getStringExtra("message")).trim(), context);
                    if (bluResult != null) {
                        if (bluResult.code.equals("0")) {
                            if(Float.parseFloat(bluResult.speed) > 0){
                                isRun = true;
                            }else {
                                isRun = false;
                            }
                        }
                    }
                }catch (Exception e){

                }
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
